package javase.io;

import java.io.*;

/**
 * 读取文件(分段读取)
 * @author LiShaowen
 * @date 2020/9/8 21:01
 */
public class FileInPutTest2 {
    public static void main(String[] args) {
        // 1.创建源
        File file = new File("src/main/java/javase/io/lsw.txt");
        // 2.选择流
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            // 3.操作（分段读取文件内容）
            // 分段读取到字节数组，五个为一组
            byte[] flush = new byte[1024*50];// 50k为一组
            //byte[] flush = new byte[5]; 5个字节为一组
            int length;
            while (((length = is.read(flush)) != -1)) {
                System.out.println(new String(flush,0,length));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != is) {
                try {
                    // 关闭资源
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }
        /*// 1.创建源
        File file = new File("src/main/java/javase/io/lsw.txt");
        // 2.选择流
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            // 3.操作（读取）
            int temp;
            while ((temp = is.read()) != -1) {
                System.out.print((char)temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/
}
